package org.jeecg.modules.lab.items.record.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.lab.entrust.entity.LabEntrustItems;
import org.jeecg.modules.lab.entrust.entity.LabItemsRecordSample;
import org.jeecg.modules.lab.items.item.service.impl.LabItemsBasicServiceImpl;
import org.jeecg.modules.lab.items.temp.entity.LabItemsTempSample;
import org.jeecg.modules.lab.items.record.mapper.LabItemsRecordSampleMapper;
import org.jeecg.modules.lab.items.record.service.ILabItemsRecordSampleService;
import org.jeecg.modules.lab.items.temp.mapper.LabItemsTempSampleMapper;
import org.jeecg.modules.lab.items.temp2.entity.LabItemsTemp2Basic;
import org.jeecg.modules.lab.items.temp2.mapper.LabItemsTemp2BasicMapper;
import org.jeecg.modules.lab.items.temp2.service.impl.LabItemsTemp2BasicServiceImpl;
import org.jeecg.modules.lab.items.vo.LabRecordSam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * @Description: 项目检测记录_样品信息
 * @Author: jeecg-boot
 * @Date: 2023-10-08
 * @Version: V1.0
 */
@Service
public class LabItemsRecordSampleServiceImpl extends ServiceImpl<LabItemsRecordSampleMapper, LabItemsRecordSample> implements ILabItemsRecordSampleService {

    @Autowired
    private LabItemsTempSampleMapper labItemsTempSampleMapper;

    @Autowired
    private LabItemsRecordSampleMapper labItemsRecordSampleMapper;

    @Autowired
    private LabItemsTemp2BasicMapper labItemsTemp2BasicMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertSamData(LabEntrustItems labEntrustItems) {
        String task_id = labEntrustItems.getEntrustId();
        String task_item_id= labEntrustItems.getId();
        String item_id = labEntrustItems.getItemId();
        QueryWrapper<LabItemsTempSample> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("item_id", item_id)
                .orderByAsc("field_sort");
        List<LabItemsTempSample> labItemsTempSampleList = labItemsTempSampleMapper.selectList(queryWrapper);
        for (LabItemsTempSample labItemsTempSample : labItemsTempSampleList) {
            // TODO: 处理每个实例 labItemsTempSample
            LabItemsRecordSample labItemsRecordSample = new LabItemsRecordSample();
            labItemsRecordSample.setTaskId(task_id);
            labItemsRecordSample.setTaskItemId(task_item_id);
            labItemsRecordSample.setItemId(item_id);
            labItemsRecordSample.setFieldName(labItemsTempSample.getFieldName());
            labItemsRecordSample.setFieldCode(labItemsTempSample.getFieldCode());
            labItemsRecordSample.setFieldValue(labItemsTempSample.getFieldValue());
            labItemsRecordSample.setFieldInput(labItemsTempSample.getFieldInput());
            labItemsRecordSample.setFieldSort(labItemsTempSample.getFieldSort());
            labItemsRecordSample.setFieldType(labItemsTempSample.getFieldType());
            labItemsRecordSample.setFieldRow(labItemsTempSample.getFieldRow());
            labItemsRecordSample.setFieldCol(labItemsTempSample.getFieldCol());
            labItemsRecordSample.setFieldRowspan(labItemsTempSample.getFieldRowspan());
            labItemsRecordSample.setFieldColspan(labItemsTempSample.getFieldColspan());
            labItemsRecordSample.setSynFlag("0");
            labItemsRecordSampleMapper.insert(labItemsRecordSample);
        }

    }

    @Override
    public List<LabRecordSam> handleSamData(String entrustId,String taskItemId, String itemId) {
        List<LabRecordSam> recordSamList = labItemsRecordSampleMapper.getRecordSamList(entrustId, itemId);
        QueryWrapper<LabItemsRecordSample> queryWrapper = new QueryWrapper<>();
        for (LabRecordSam labRecordSam : recordSamList) {
            queryWrapper.clear();
            if(!"".equals(taskItemId) && taskItemId !=null){
                queryWrapper.eq("task_item_id",taskItemId);
            }
            queryWrapper.eq("field_row", labRecordSam.getSId())
                    .eq("task_id", entrustId)
                    .eq("item_id", itemId)
                    .orderByAsc("field_sort");
            List<LabItemsRecordSample> itemsRecordSampleList = labItemsRecordSampleMapper.selectList(queryWrapper);
            labRecordSam.setItemsRecordSampleList(itemsRecordSampleList);
        }
        return recordSamList;
    }

    @Override
    public List<LabItemsRecordSample> handleSamDataApp(String entrustId,String taskItemId, String itemId) {
        QueryWrapper<LabItemsRecordSample> queryWrapper = new QueryWrapper<>();
        queryWrapper.clear();
        queryWrapper.eq("task_id", entrustId)
                .eq("item_id", itemId)
                .eq("task_item_id",taskItemId)
                .ne("field_input", "0")
                .orderByAsc("field_sort");
        List<LabItemsRecordSample> itemsRecordSampleList = labItemsRecordSampleMapper.selectList(queryWrapper);
        return itemsRecordSampleList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(String entrustId,String taskitemId, String itemId, List<LabRecordSam> recordSamList) {
        //1、先根据委托/监督Id、项目id条件删除数据
        QueryWrapper<LabItemsRecordSample> queryWrapper = new QueryWrapper<>();
        queryWrapper.clear();
        queryWrapper.eq("task_id", entrustId)
                .eq("task_item_id",taskitemId)
                .eq("item_id", itemId);
        labItemsRecordSampleMapper.delete(queryWrapper);
        //2、重新插入数据
        if (recordSamList != null && recordSamList.size() > 0) {
            for (LabRecordSam recordSams : recordSamList) {
                for (LabItemsRecordSample sample : recordSams.getItemsRecordSampleList()) {
                    if ("check_time".equals(sample.getFieldCode()) && "1".equals(sample.getFieldInput())) {
                        String str = sample.getFieldValue();
                        if(!str.contains("至")){
                            try {
                                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                                Date oldDate = format.parse(str);
                                Date newDate = format.parse(DateUtils.getDate("yyyy-MM-dd"));
                                if(!oldDate.equals(newDate)){
                                    sample.setFieldValue(str+"至"+DateUtils.getDate("yyyy-MM-dd"));
                                }
                            }catch (ParseException e){
                                e.printStackTrace();
                            }
                        }
                    }
                    labItemsRecordSampleMapper.insert(sample);
                }
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateApp(String entrustId, String itemId, List<LabItemsRecordSample> itemsRecordSampleList) {
        //1、更新样品信息数据
        if (itemsRecordSampleList != null && itemsRecordSampleList.size() > 0) {
            for (LabItemsRecordSample sample : itemsRecordSampleList) {
                labItemsRecordSampleMapper.updateById(sample);
            }
        }
    }

    /**
     * 根据 委托id和项目id
     *
     * @param itemId
     * @return
     */
    @Override
    public List<LabItemsRecordSample> selectByMainIds(String entrustId, String itemId) {
        QueryWrapper<LabItemsRecordSample> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("task_id", entrustId)
                .eq("item_id", itemId)
                .eq("field_input", "1");
        //List<LabItemsRecordSample> labItemsRecordSamples = labItemsRecordSampleMapper.selectByMainIds(itemId);
        List<LabItemsRecordSample> labItemsRecordSamples = labItemsRecordSampleMapper.selectList(queryWrapper);
        return labItemsRecordSamples;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void kyInsertSamData(LabEntrustItems labEntrustItems) {
        String task_id = labEntrustItems.getEntrustId();
        String item_id = labEntrustItems.getItemId();
        String task_item_id = labEntrustItems.getId();

        //先删历史数据
        QueryWrapper<LabItemsRecordSample> wrapper = new QueryWrapper<>();
        wrapper.eq("item_id", item_id);
        labItemsRecordSampleMapper.delete(wrapper);

        QueryWrapper<LabItemsTemp2Basic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("items_id", item_id).orderByAsc("field_sort");
        List<LabItemsTemp2Basic> labItemsTemp2BasicList = labItemsTemp2BasicMapper.selectList(queryWrapper);
        //判断labItemsTemp2BasicList是否为双数，用于处理最后条数据是否合并列。
        int size = labItemsTemp2BasicList.size();
        boolean bool = false;
        if ((size / 2) > 0) {
            bool = true;
        }
        //循环生成sampl数据
        for (int i = 0; i < size; i++) {
            // 计算整数
            int row = i / 2;
            // 计算余数
            int clo = i % 2;
            LabItemsTemp2Basic basic = labItemsTemp2BasicList.get(i);
            String field_name = basic.getFieldName();
            //插入名称列
            LabItemsRecordSample sampa = new LabItemsRecordSample();
            sampa.setTaskId(task_id);
            sampa.setItemId(item_id);
            sampa.setTaskItemId(task_item_id);
            sampa.setFieldName(field_name);
            sampa.setFieldInput("0");
            sampa.setFieldSort(i + 1);
            sampa.setFieldType("String");
            sampa.setFieldRow(row);
            sampa.setFieldCol(clo * 2 + 1);
            sampa.setFieldRowspan(0);
            sampa.setFieldColspan(0);
            sampa.setSynFlag("0");
            labItemsRecordSampleMapper.insert(sampa);
            //插入输入列
            LabItemsRecordSample sampb = new LabItemsRecordSample();
            sampb.setTaskId(task_id);
            sampb.setItemId(item_id);
            sampb.setTaskItemId(task_item_id);
            sampb.setFieldName(field_name);
            sampb.setFieldCode("sam_value" + (i + 1));
            sampb.setFieldInput("1");
            sampb.setFieldSort(i + 2);
            sampb.setFieldType("String");
            sampb.setFieldRow(row);
            sampb.setFieldCol(clo * 2 + 2);
            sampb.setFieldRowspan(0);
            sampb.setFieldColspan(0);
            sampb.setSynFlag("0");
            if (bool && (size - 1) == i && i >= 2) {
                sampb.setFieldColspan(3);
            }
            labItemsRecordSampleMapper.insert(sampb);
        }
    }

    @Override
    public void deleteByTaskId(String taskId) {
        labItemsRecordSampleMapper.deleteByTaskId(taskId);
    }
}
